package cn.joker.services.impl;


import cn.hutool.core.util.StrUtil;
import cn.joker.constant.PageQuery;
import cn.joker.constant.PageResult;
import cn.joker.constant.Result;
import cn.joker.entity.po.Systems;
import cn.joker.mapper.SystemsMapper;
import cn.joker.services.ISystemsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 接入的系统应用表 服务实现类
 * </p>
 *
 * @author joker
 * @since 2025-04-29
 */
@Service
public class SystemsServiceImpl extends ServiceImpl<SystemsMapper, Systems> implements ISystemsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSystemInfo(Systems systems) {
        if (StrUtil.isEmpty(systems.getSystemCode())) {
            systems.setSystemCode(StrUtil.uuid());
            systems.setCreateTime(LocalDateTime.now());
            systems.setUpdateTime(null);
            save(systems);
        } else {
            systems.setUpdateTime(LocalDateTime.now());
            updateById(systems);
        }
    }

    @Override
    public Result<PageResult<Systems>> getSystemList(PageQuery query) {
        Page<Systems> page = lambdaQuery().page(new Page<>(query.getPageNum(), query.getPageSize()));
        PageResult<Systems> systemsPageResult = new PageResult<>(page);
        return Result.success(systemsPageResult);
    }

    @Override
    public Result<String> deleteSystemByCode(Systems systemCode) {
        removeById(systemCode);
        return Result.success("删除成功！");
    }
}
